/*
 * @Author       : xiangmin
 * @File         : 浮点数计算
 * @Date         : 2023-08-07 19:54:55
 * @LastEditors  : xiangmin
 * @LastEditTime : 2023-08-09 13:48:55
 */
import Big from 'big.js'

type Args = (number | string)[]
class Calc {
	private calculate(args: Args, method: string) {
		try {
			return args
				.slice(1)
				.reduce((prev, next) => prev[method](next), Big(args[0]))
				.toNumber()
		} catch (error) {
			return error
		}
	}

	/**
	 * [plus 累加]
	 */
	public plus = (...args: Args): number => this.calculate(args, 'plus')

	/**
	 * [plus 累减]
	 */
	public minus = (...args: Args): number => this.calculate(args, 'minus')

	/**
	 * [plus 累乘]
	 */
	public times = (...args: Args): number => this.calculate(args, 'times')

	/**
	 * [plus 累除]
	 */
	public div = (...args: Args): number => this.calculate(args, 'div')
}

export default new Calc()
